package com.rovinj_server.receiver;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.rovinj_server.app.MyApplication;


/**
 * Created by Test on 2017/1/6.
 */
public class KeyReceiver extends BroadcastReceiver {
    private long keyDownTime = 0;

    private KeyReceiverListener keyReceiverListener;

    public void setKeyReceiverListener(KeyReceiverListener keyReceiverListener) {
        this.keyReceiverListener = keyReceiverListener;
    }

    @Override
    public void onReceive(final Context context, final Intent intent) {
        boolean keyDown = intent.getBooleanExtra("keydown", false);
        MyApplication myApp = MyApplication.getInstance();
        if (keyDown) {
            Activity activity = myApp.getCurrentActivity();
            if(activity!=null && activity instanceof KeyReceiverListener){
                final KeyReceiverListener keyReceiverActivity = (KeyReceiverListener)activity;
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        keyReceiverActivity.onReceive(context, intent);
                    }
                }.start();
            }
        }
    }
}